//@author A0085897A

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Print {
	
	// METHODS
	// Print a table row containing cells of the specified cell widths
	public void row(String[] cells, int[] cellWidths) {
		if (cells.length != cellWidths.length) {
			throw new IllegalArgumentException();
		}
		StringBuilder sb = new StringBuilder();
		List<List<String>> list = new ArrayList<List<String>>();
		for (int i=0; i<cells.length; i++) {
			list.add(this.wrapString(cells[i], cellWidths[i]-1));
		}
		int line = 0;
		boolean done;
		do {
			done = true;
			for (int i=0; i<list.size(); i++) {
				List<String> cell = list.get(i);
				String temp;
				if (line < cell.size()) {
					temp = cell.get(line);
				} else {
					temp = "";
				}
				sb.append(String.format("%-" + cellWidths[i] + "s", temp));
				if (line < cell.size()-1) {
					done = false;
				}
			}
			this.centreAlign(sb.toString());
			sb.setLength(0);
			line++;
		} while(!done);
	}
	// Break the given string into a list of strings, with the length of each
	// string not greater than the specified width
	private List<String> wrapString(String str, int width) {
		String[] words = str.trim().split(" ");
		List<String> result = new ArrayList<String>();
		StringBuilder line = new StringBuilder();
		for (String word: words) {
			if (word.length() <= width) {
				if (line.length() + word.length() + 1 > width) {
					result.add(line.toString());
					line.setLength(0);
				}
				if (line.length() == 0) {
					line.append(word);
				} else {
					line.append(" " + word);
				}
			} else {
				if (line.length() != 0) {
					if (line.length() + 1 < width) {
						int endIndex = width - line.length() - 1;
						System.out.println(endIndex);
						line.append(" " + word.substring(0, endIndex));
						word = word.substring(endIndex);
						System.out.println(word);
					}
					result.add(line.toString());
					line.setLength(0);
				}
				while (word.length() > width) {
					result.add(word.substring(0, width));
					word = word.substring(width);
				}
				line.append(word);
			}
		}
		result.add(line.toString());
		return result;
	}
	// Print the contents of the specified file, with a flag to centre-align the text
	public void file(String fileName, boolean centreAlign) {
		File file = new File(fileName);
		if (file.isFile() && file.exists()) {
			this.blankLine();
			try {
				InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
				BufferedReader br = new BufferedReader(isr);
				String line;
				while ((line = br.readLine()) != null) {
					if (centreAlign) {
						this.centreAlign(line);
					} else {
						this.leftAlign(line);
					}
				}
				isr.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			this.blankLine();
		}
	}
	// Print the given command
	public void command(String str) {
		this.separator();
		this.blankLine();
		this.centreAlign(this.spaceOut(str.toUpperCase()));
		this.blankLine();
		this.separator();
	}
	// Print the specified string, left-aligned
	public void leftAlign(String str) {
		System.out.println(String.format("#%3s%-100s%3s#", "", str, ""));
	}
	// Print the specified string, centre-aligned
	public void centreAlign(String str) {
		int spaces = 106 - str.length();
		int prepend = spaces / 2;
		int append = spaces - prepend;
		System.out.println(String.format("#%" + prepend + "s%s%" + append + "s#", " ", str, " "));
	}
	// Print a blank line
	public void blankLine() {
		System.out.println(String.format("#%106s#", ""));
	}
	// Print separator
	public void separator() {
		StringBuilder sb = new StringBuilder();
		for (int i=0; i<108; i++) {
			sb.append("#");
		}
		System.out.println(sb.toString());
	}
	public void separator(char c) {
		StringBuilder sb = new StringBuilder();
		for (int i=0; i<100; i++) {
			sb.append(c);
		}
		System.out.println("#   " + sb.toString() + "   #");
	}
	
	// Print the prompt
	public void prompt(String str) {
		System.out.print(str + ":\n> ");
	}

	// Print feedback
	public void feedback(String str) {
		System.out.println(str);
	}

	// Returns the given string with a space inserted between each character
	public String spaceOut(String str) {
		StringBuilder sb = new StringBuilder();
		sb.append(str.charAt(0));
		for (int i=1; i<str.length(); i++) {
			sb.append(" " + str.charAt(i));
		}
		return sb.toString();
	}

	// Format the specified money value
	public String formatMoney(double value) {
		String str = String.format("%-3.2f", value);
		if (value > 0) {
			str = "+" + str;
		}
		return str;
	}

	// Capitalise the specified string (ie. all lowercase except the first character)
	public String capitalise(String str) {
		str = str.trim();
		return Character.toUpperCase(str.charAt(0)) + str.substring(1).toLowerCase();
	}

}
